package com.xiaodu.acer.phoneplay.presenter;

import com.xiaodu.acer.phoneplay.bean.SearchHistoryBean;
import com.xiaodu.acer.phoneplay.bean.SearchResult;
import com.xiaodu.acer.phoneplay.common.rx.RxHttpReponseCompat;
import com.xiaodu.acer.phoneplay.common.rx.subscriber.ProgressSubcriber;
import com.xiaodu.acer.phoneplay.data.dao.SearchHistoryDao;
import com.xiaodu.acer.phoneplay.presenter.contract.SearchContract;
import com.xiaodu.acer.phoneplay.ui.base.BasePresenter;

import java.util.List;

import javax.inject.Inject;


/**
 * @author 小杜
 *         邮箱:m15701207383@163.com
 * @version V1.0
 * @Description: ${TODO}(用一句话描述该文件做什么)
 * @date 2018/1/12 11:00
 */

public class SearchPresenter extends BasePresenter<SearchContract.Model, SearchContract.View> {

    @Inject
    public SearchPresenter(SearchContract.Model model, SearchContract.View view) {
        super(model, view);
    }

    public void search(String keyword) {
        mModel.search(keyword)
                .compose(RxHttpReponseCompat.<SearchResult>compatResult())
                .subscribe(new ProgressSubcriber<SearchResult>(mContext, mView) {
                    @Override
                    public void onNext(SearchResult searchResult) {
                        mView.showSearchResult(searchResult);
                    }
                });
    }

    public void getSuggestion(String keyword) {
        mModel.getSuggestion(keyword)
                .compose(RxHttpReponseCompat.<List<String>>compatResult())
                .subscribe(new ProgressSubcriber<List<String>>(mContext, mView) {
                    @Override
                    public void onNext(List<String> strings) {
                        mView.showSuggestions(strings);
                    }
                });
    }

    /**
     * 保存查询记录到数据库中
     *
     * @param keyword 历史记录
     */
    public void saveSearchHistory(String keyword) {
        SearchHistoryBean bean = new SearchHistoryBean();
        bean.setName(keyword);
        SearchHistoryDao.insert(bean);
    }

    /**
     * 查询历史数据
     */
    public void showHistory() {
        List<SearchHistoryBean> list = SearchHistoryDao.query();
        mView.showSearchHistory(list);
    }

    /**
     * 删除历史查询数据
     */
    public void deleteHistory() {
        SearchHistoryDao.deleteAll();
        mView.deleteSearchHistory();
    }
}
